package cn.xz.tipsum.utils


import android.graphics.drawable.Drawable
import android.os.Build
import android.provider.Settings
import android.util.TypedValue
import androidx.core.content.ContextCompat
import cn.xz.tipsum.base.BaseApplication
import java.util.*


object AppHelper {

    /**
     * dp转px
     *
     * @param dpVal
     * @return
     */
    fun dp2px(dpVal: Int): Int {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            dpVal.toFloat(),
            BaseApplication.getContext().resources.displayMetrics
        ).toInt()
    }

    /**
     * dp转px
     *
     * @param dpVal
     * @return
     */
    @JvmStatic
    fun dp2px(dpVal: Float): Int {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            dpVal,
            BaseApplication.getContext().resources.displayMetrics
        ).toInt()
    }

    fun getDrawable(id: Int): Drawable?{
        return ContextCompat.getDrawable(BaseApplication.getContext(),id)
    }


    fun getUUID(): String{
        var uuid = getAndroidID()
        if (uuid.isNotEmpty()){
            uuid = "实时金额" + UUID.nameUUIDFromBytes(uuid.toByteArray()).toString().replace("-", "")
        }
        return uuid
    }

    private fun getAndroidID(): String {
        var id = "";
        if (BaseApplication.mContext != null){
            id = Settings.Secure.getString(BaseApplication.mContext!!.getContentResolver(), Settings.Secure.ANDROID_ID)
        }
        return id
    }

    //是不是小米机型
    fun isMIUI(): Boolean {
        val manufacturer = Build.MANUFACTURER
        //这个字符串可以自己定义,例如判断华为就填写huawei,魅族就填写meizu
        return "xiaomi".equals(manufacturer, ignoreCase = true)
    }
}